<?php
/**
 * Product: rdc_analysis.
 * Date: 18-7-25
 * Time: 下午6:02
 */

namespace server;

trait ErrorHandler
{
    public function handleFatal()
    {
        $error = error_get_last();

        if(isset($error['type']))
        {
            switch($error['type'])
            {
                case E_ERROR:
                case E_PARSE:
                case E_CORE_ERROR:
                case E_COMPILE_ERROR:
                    $message = $error['message'];
                    $file = $error['file'];
                    $line = $error['line'];
                    $log = "$message ($file:$line)\nStack trace:\n";
                    $trace = debug_backtrace();

                    foreach($trace as $i => $t)
                    {
                        if(!isset($t['file']))
                        {
                            $t['file'] = 'unknown';
                        }

                        if(!isset($t['line']))
                        {
                            $t['line'] = 0;
                        }

                        if(!isset($t['function']))
                        {
                            $t['function'] = 'unknown';
                        }

                        $log .= "#$i {$t['file']}({$t['line']}): ";

                        if(isset($t['object']) and is_object($t['object']))
                        {
                            $log .= get_class($t['object']) . '->';
                        }
                        $log .= "{$t['function']}()\n";
                    }

                    if(isset($_SERVER['REQUEST_URI']))
                    {
                        $log .= '[QUERY] ' . $_SERVER['REQUEST_URI'];
                    }

                    self ::$response -> end($log);
                // 					$this->application->logger->info('error log: ' . $log);
                // 					self::$response->status( 500 );
                // 					self::$response->end( 'Internal Server Errors' );

                default:
                    break;
            }
        }
    }
}